package com.mcodesal.framework.utils;

import javax.microedition.khronos.opengles.GL10;



import android.graphics.Paint;
import android.os.SystemClock;

/**
 * This class this class initializes its own LabelMaker and calculates
 * MsPF.
 * 
 * @author Matias
 *
 */
public class MsPerFrameCounter {

	private Paint mLabelPaint;
	private LabelMaker mLabels;
	public NumericSprite mHighestValue;
	public NumericSprite mAverageValue;
	public NumericSprite mLowestValue;
	private int mAverageLabel;
	private int mLowestLabel;
	private int mHighestLabel;
	private int mHigh = 0;
	private int mLow = 10000;
	private int mScreenWidth;
	private int mScreenHeight;
	private long mLastTime = 0;
	public int delta;
	
	public MsPerFrameCounter() {
		mLabelPaint = new Paint();
        mLabelPaint.setTextSize(16);
        mLabelPaint.setAntiAlias(true);
        mLabelPaint.setARGB(0xff, 0xff, 0x00, 0x00);      
		
	}
	/**
	 * Goes inside onSurfaceChanged method in our GameRenderer.
	 * @param gl
	 */
	public void initialize(GL10 gl, int width, int height) {
		mScreenWidth = width;
		mScreenHeight = height;
		if (mLabels != null) {
            mLabels.shutdown(gl);
        } else {
            mLabels = new LabelMaker(true, 256, 64);
        }
		mLabels.initialize(gl);
		mLabels.beginAdding(gl);
        mAverageLabel = mLabels.add(gl, "Ms/F (A):", mLabelPaint);
        mHighestLabel = mLabels.add(gl, "Ms/F (H):", mLabelPaint);
        mLowestLabel = mLabels.add(gl, "Ms/F (L):", mLabelPaint);
        mLabels.endAdding(gl);
        
        if (mAverageValue != null) {
            mAverageValue.shutdown(gl);
        } else {
            mAverageValue = new NumericSprite();
            mAverageValue.setValue(0);
        }
        mAverageValue.initialize(gl, mLabelPaint);
        
        if (mLowestValue != null) {
            mLowestValue.shutdown(gl);
        } else {
            mLowestValue = new NumericSprite();
            mLowestValue.setValue(10000);
        }
        mLowestValue.initialize(gl, mLabelPaint);
        
        if (mHighestValue != null) {
            mHighestValue.shutdown(gl);
        } else {
            mHighestValue = new NumericSprite();
            mHighestValue.setValue(0);
        }
        mHighestValue.initialize(gl, mLabelPaint);


	}
	
	public void draw(GL10 gl) {
		
		update();
		
		mLabels.beginDrawing(gl, mScreenWidth, mScreenHeight);
		mLabels.draw(gl, 0, 30, mHighestLabel);
		mLabels.draw(gl, 0, 15, mAverageLabel);
		mLabels.draw(gl, 0, 0, mLowestLabel);
        mLabels.endDrawing(gl);
        
        mHighestValue.draw(gl, 70, 30, mScreenWidth, mScreenHeight);
        mAverageValue.draw(gl, 70, 15, mScreenWidth, mScreenHeight);
        mLowestValue.draw(gl, 70, 0, mScreenWidth, mScreenHeight);
        
	}
	public void update() {
		long time = SystemClock.uptimeMillis();
		if (mLastTime == 0) {
            mLastTime = time;
        }
		delta = (int)(time - mLastTime);
		
		if(mHigh < delta){
			mHighestValue.setValue((int)delta);
			mHigh = delta;
		}
		mAverageValue.setValue((int)delta);
		if(mLow > delta && delta != 0){
			mLowestValue.setValue((int)delta);
			mLow = delta;
		}
		mLastTime = time;
		
	}
}
